package niit.wei.service.impl;

import lombok.extern.slf4j.Slf4j;
import niit.wei.service.BaiDuIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author WeiJinLong
 * @Date 2023-08-16 22:11
 * @Version 1.0
 */
@Service
@Slf4j
public class BaiDuIndexServiceImpl implements BaiDuIndexService {

    @Resource(name = "redisTemplate")
    HashOperations<String, String, Object> ops;
    @Override
    public Map<String,Object> getTimes(String key) {
        ops.increment(key,dateFormat(),1);
        Map<String, Object> hashMap = ops.entries(key);
        Set<String> entries = hashMap.keySet();
        Collection<Object> values = hashMap.values();
        Map<String, Object> map = new HashMap<>();
        map.put("times",entries);
        map.put("counts",values);
        return map;
    }

    public String dateFormat(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = sdf.format(new Date());
        return format;
    }
}
